package com.example.disk.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.disk.domain.entity.User;
import com.example.disk.mapper.UserMapper;
import com.example.disk.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.disk.util.Email_Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.net.HttpCookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author auto
 * @since 2020-07-15
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public boolean sendEmail(String email, int captcha) {
        return Email_Util.send(email,captcha);
    }

    //检测是否存在该邮箱
    @Override
    public boolean hasEmail(String email) {
        //查询数据库中这个email有没有注册
        Map<String,Object> condition = new HashMap<>();
        condition.put("email",email);
        List<User> list = userMapper.selectByMap(condition);
        return list.size()>0;
    }

    //检测是否存在该账号
    @Override
    public boolean hasUsername(String username) {
        //检查此账号是否已经注册
        Map<String,Object> condition = new HashMap<>();
        condition.put("username",username);
        List<User> list = userMapper.selectByMap(condition);
        return list.size()>0;
    }

    //插入一条用户数据
    @Override
    public int insertOne(User user) {
        return userMapper.insert(user);
    }

    //用户名和账号是否存在
    @Override
    public boolean hasUserNameAndPsw(String username, String password) {
        Map<String,Object> condition = new HashMap<>();
        condition.put("username",username);
        condition.put("password",password);
        List<User>list = userMapper.selectByMap(condition);
        return list.size()>0;
    }

    //通过用户名得到用户信息
    @Override
    public User getUserInfoByUserName(String username) {
        Map<String,Object> map = new HashMap<>();
        map.put("username",username);
        List<User> list = userMapper.selectByMap(map);
        if(list.size()>0){
            return (User)list.get(0);
        }else{
            return null;
        }
    }

    //注销账号
    @Override
    public void logout(HttpSession session) {
        session.setAttribute("login",false);
    }

    //修改信息
    @Override
    public boolean revise(int userId,String nickname, String password) {
        User user = new User();
        user.setId(userId);
        user.setNickname(nickname);
        user.setPassword(password);
        int flag=userMapper.updateById(user);
        return flag>0;
    }

    @Override
    public int getTotalSize(int userId) {

        return userMapper.selectOne(new QueryWrapper<User>().eq("id",userId)).getSize();
    }

    @Override
    public int getUsedSize(int userId) {
        return userMapper.selectOne(new QueryWrapper<User>().eq("id",userId)).getUsedSize();
    }


}
